import gdata.docs.data
import gdata.docs.client
import gdata.spreadsheet.service
import sys, os

c = gdata.spreadsheet.service.SpreadsheetsService()
client = gdata.docs.client.DocsClient()
client.ssl = True
c.email = 'science.uoit@gmail.com'
c.password = sys.argv[1]
c.ProgrammaticLogin()
client.ClientLogin(email='science.uoit@gmail.com',
                   password=sys.argv[1],
                   source="uoit-pages")

## Create a folder

(account, version) = sys.argv[2:4]

feed = client.GetDocList(uri="/feeds/default/private/full/-/folder")
folder = None
for folder in feed.entry:
  if folder.title.text == 'pages - %s - %s' % (account, version):
    print folder.title.text
    break

if not folder:
  print "Not found."
  sys.exit(0)

for a in os.listdir("."):
  if a.endswith(".xls"):
    print a[:-4]
    f = open(a, "r")
    ms = gdata.data.MediaSource(
            file_handle=f,
            content_type='application/vnd.ms-excel',
            content_length=os.path.getsize(a))
    entry = client.Upload(ms, a[:-4], folder_or_uri=folder.content.src+"?convert=true")
    print entry.content.src
